<?php


namespace App\Store\Controllers\Sysset\Commission;

use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Sysset\Commission\Priceandfee;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\Sysset\Commission\Priceandfee as ValidatePriceandfee;

/**
 * 金价+工费提成
 */
class PriceandfeeController extends Controller
{
    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_ids', 'goods_category_names', 'preferential_min', 'preferential_max', 'glodprice_min', 'glodprice_max', 'fee_min', 'fee_max', 'commission_rate', 'commission_rate_1', 'commission_rate_2', 'is_method', 'commission_money_1', 'commission_money_2']);
        $Validate = new ValidatePriceandfee('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $store_ids = $req_data['store_ids'];
        $store_ids = is_array($store_ids) ? $store_ids : (is_array(json_decode($store_ids, true)) ? json_decode($store_ids, true) : []);

        $goods_category_names = $req_data['goods_category_names'];
        $goods_category_names = is_array($goods_category_names) ? $goods_category_names : (is_array(json_decode($goods_category_names, true)) ? json_decode($goods_category_names, true) : []);

        // 组装数据
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        foreach ($store_ids as $store_id) {
            $priceandfee = [];
            foreach ($goods_category_names as $k => $goods_category_name) {
                $one = GoodsCategory::where([
                    'store_id' => $store_id,
                    'category_name' => $goods_category_name,
                    'is_del'=>1
                ])->first();
                if ($one) {
                    $one = $one->toArray();
                    $goods_category_id = $one['id'];

                    $info =Priceandfee::query()
                        ->where('goods_category_id',$goods_category_id)
                        ->where('store_id',$store_id)
                        ->where('is_del',1)
                        ->first();
                    if(!empty($info)){
                        return response()->json(['code' => '001', 'msg' => '该类别已经设置了提成，无法多次设置']);
                    }

                    $priceandfee[$k]['cp_id'] = $cp_id;
                    $priceandfee[$k]['store_id'] = $store_id;
                    $priceandfee[$k]['goods_category_id'] = $goods_category_id;
                    $priceandfee[$k]['preferential_min'] = !empty($req_data['preferential_min']) ? $req_data['preferential_min'] : 0;
                    $priceandfee[$k]['preferential_max'] = !empty($req_data['preferential_max']) ? $req_data['preferential_max'] : 0;
                    $priceandfee[$k]['glodprice_min'] = !empty($req_data['glodprice_min']) ? $req_data['glodprice_min'] : 0;
                    $priceandfee[$k]['glodprice_max'] = !empty($req_data['glodprice_max']) ? $req_data['glodprice_max'] : 0;
                    $priceandfee[$k]['fee_min'] = !empty($req_data['fee_min']) ? $req_data['fee_min'] : 0;
                    $priceandfee[$k]['fee_max'] = !empty($req_data['fee_max']) ? $req_data['fee_max'] : 0;
                    $priceandfee[$k]['commission_rate'] = !empty($req_data['commission_rate']) ? $req_data['commission_rate'] : 0;
                    $priceandfee[$k]['commission_rate_1'] = !empty($req_data['commission_rate_1']) ? $req_data['commission_rate_1'] : 0;
                    $priceandfee[$k]['commission_rate_2'] = !empty($req_data['commission_rate_2']) ? $req_data['commission_rate_2'] : 0;
                    $priceandfee[$k]['create_account_id'] = $accountinfo['id'];
                    $priceandfee[$k]['create_account_real_name'] = $accountinfo['real_name'];
                    $priceandfee[$k]['create_at'] = time();
                    $priceandfee[$k]['is_method'] = $req_data['is_method'] ?? 1;
                    $priceandfee[$k]['commission_money_1'] = $req_data['commission_money_1'] ?? 0;
                    $priceandfee[$k]['commission_money_2'] = $req_data['commission_money_2'] ?? 0;
                }
                if ($priceandfee) Priceandfee::insert($priceandfee);
            }
        }
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = Priceandfee::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {
        $one = Priceandfee::where('id', $request['id'])->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'store_id', 'goods_category_id', 'preferential_min', 'preferential_max', 'glodprice_min', 'glodprice_max', 'fee_min', 'fee_max', 'commission_rate', 'commission_rate_1', 'commission_rate_2', 'is_method', 'commission_money_1', 'commission_money_2']);
        $Validate = new ValidatePriceandfee('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);

        $req_data['preferential_min'] = !empty($req_data['preferential_min']) ? $req_data['preferential_min'] : 0;
        $req_data['preferential_max'] = !empty($req_data['preferential_max']) ? $req_data['preferential_max'] : 0;
        $req_data['glodprice_min'] = !empty($req_data['glodprice_min']) ? $req_data['glodprice_min'] : 0;
        $req_data['glodprice_max'] = !empty($req_data['glodprice_max']) ? $req_data['glodprice_max'] : 0;
        $req_data['fee_min'] = !empty($req_data['fee_min']) ? $req_data['fee_min'] : 0;
        $req_data['fee_max'] = !empty($req_data['fee_max']) ? $req_data['fee_max'] : 0;
        $req_data['commission_rate'] = !empty($req_data['commission_rate']) ? $req_data['commission_rate'] : 0;
        $req_data['commission_rate_1'] = !empty($req_data['commission_rate_1']) ? $req_data['commission_rate_1'] : 0;
        $req_data['commission_rate_2'] = !empty($req_data['commission_rate_2']) ? $req_data['commission_rate_2'] : 0;
        $req_data['update_account_id'] = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at'] = time();
        $req_data['is_method'] = $req_data['is_method'] ?? 1;
        $req_data['commission_money_1'] = $req_data['commission_money_1'] ?? 0;
        $req_data['commission_money_2'] = $req_data['commission_money_2'] ?? 0;


        $info =Priceandfee::query()
            ->where('goods_category_id',$req_data['goods_category_id'])
            ->where('id','<>',$request['id'])
            ->where('store_id',$req_data['store_id'])
            ->where('is_del',1)
            ->first();
        if(!empty($info)){
            return response()->json(['code' => '001', 'msg' => '该类别已经设置了提成，无法多次设置']);
        }

        Priceandfee::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidatePriceandfee('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['is_del'] = 2;
        $req_data['update_account_id'] = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at'] = time();

        Priceandfee::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
